home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 11 - Porting / Sample Code / MemAlloc.c < prev    next >
Text File  |  1995-03-28  |  5KB  |  180 lines

  1. /************************************************************************************
  2.  *                                                                                                                                                                    *
  3.  *    MemAlloc.c                                                                                                                                            *
  4.  *                                                                                                                                                                    *
  5.  *    ©1995 Douglas Grounds. All Rights Reserved.                                                                            *
  6.  *                                                                                                                                                                    *
  7.  *    This file contains memory allocation functions which are quite handy.                        *
  8.  *                                                                                                                                                                    *
  9.  ************************************************************************************/
  10.  
  11.  #include <Memory.h>
  12.  #include <Types.h>
  13.  
  14.  #ifndef TRUE
  15.  #define TRUE        1
  16.  #endif
  17.  
  18.  #ifndef FALSE
  19.  #define FALSE    0
  20.  #endif
  21.  
  22.  #include "MemAlloc.h"
  23.  #include "ErrCheck.h"
  24.  
  25. /*-----------------------------------
  26.  *
  27.  *    Global Variables
  28.  *
  29.  *    Keeps track of how much memory
  30.  *    the application has allocated
  31.  *    vs. Resource Mgr, Toolbox, System,
  32.  *    etc.
  33.  *
  34.  *-----------------------------------*/
  35.  
  36.  long            gMemoryAllocated = 0L;            // Total memory allocated by application, in bytes.
  37.  long            gMemHandlesAllocated = 0L;    // Total handles allocated by application, in bytes.
  38.  long            gMemPointersAllocated = 0L;    // Total pointers allocated by application, in bytes.
  39.  
  40. /*-----------------------------------
  41.  *
  42.  *    Example Usage
  43.  *
  44.  *-----------------------------------*/
  45.  
  46. #if (FALSE)
  47.  
  48.     void myExampleFunction (void)
  49.     {
  50.         Handle        hndl;
  51.         
  52.         hndl = maNewHandle(1024, FALSE);
  53.         if (hndl == NULL)
  54.             return;
  55.         
  56.         ... use "hndl" ...
  57.         
  58.         hndl = maDisposeHandle(hndl);        // Sets "hndl" to NULL for safety
  59.         
  60.         myPtr = maNewPtr(1024, TRUE);
  61.         if (myPtr == NULL)
  62.             return;
  63.         
  64.         ... use "myPtr" ...
  65.         
  66.         myPtr = maDisposePtr(myPtr);        // Sets "myPtr" to NULL for safety
  67.     }
  68.     
  69. #endif
  70.  
  71. /******************************************************
  72.  *    maNewHandle.                                                                            *
  73.  *                                                                                                        *
  74.  *    Allocates a new relocateable block of memory.            *
  75.  *    Returns NULL if an error occurs and keeps track        *
  76.  *    of total memory allocated. Use MADisposeHandle        *
  77.  *    to dispose of the block once you've finished.            *
  78.  ******************************************************/
  79.  
  80. Handle maNewHandle (long sizeNeeded, Boolean clear)
  81. {
  82.     Handle        hndl;
  83.     
  84.     if (clear)
  85.         hndl = NewHandleClear(sizeNeeded);
  86.     else
  87.         hndl = NewHandle(sizeNeeded);
  88.     
  89.     if (checkMemError(hndl))
  90.         return NULL;
  91.     
  92.     gMemoryAllocated += sizeNeeded;
  93.     gMemHandlesAllocated += sizeNeeded;
  94.     
  95.     return hndl;
  96. }
  97.  
  98. /******************************************************
  99.  *    maDisposeHandle.                                                                    *
  100.  *                                                                                                        *
  101.  *    Disposes of a handle. Make sure you've used                *
  102.  *    MANewHandle to allocate to keep track of memory        *
  103.  *    correctly. Returns NULL for convenience.                    *
  104.  ******************************************************/
  105.  
  106. Handle maDisposeHandle (Handle doomedHandle)
  107. {
  108.     long        howBig;
  109.     
  110. #if __YOU_WANT_DEBUGGING
  111.     // Null, odd address or unrecognized by Memory Manager are all
  112.     // problems!
  113.     if ((doomedHandle == NULL) || (doomedHandle & 0x01) || 
  114.             (*doomedHandle == NULL) || (*doomedHandle & 0x01) || (GetHandleSize(doomedHandle) == 0L))
  115.     {
  116.         // Probably should handle this, buddy-boy.
  117.         return NULL;
  118.     }
  119. #endif
  120.  
  121.     gMemoryAllocated -= (howBig = GetHandleSize(doomedHandle));
  122.     gMemHandlesAllocated -= howBig;
  123.     DisposeHandle(doomedHandle);
  124.     return NULL;
  125. }
  126.  
  127. /******************************************************
  128.  *    maNewPtr.                                                                                    *
  129.  *                                                                                                        *
  130.  *    Allocate a new nonrelocateable block of memory.        *
  131.  *    Returns NULL if an error occurs and keeps track        *
  132.  *    of total memory allocated. Use MADisposePtr                *
  133.  *    to dispose of the block once you've finished.            *
  134.  ******************************************************/
  135.  
  136. Ptr maNewPtr (long sizeNeeded, Boolean clear)
  137. {
  138.     Ptr            myPtr;
  139.     
  140.     if (clear)
  141.         myPtr = NewPtrClear(sizeNeeded);
  142.     else
  143.         myPtr = NewPtr(sizeNeeded);
  144.     
  145.     if (checkMemError(myPtr))
  146.         return NULL;
  147.     
  148.     gMemoryAllocated += sizeNeeded;
  149.     gMemPointersAllocated += sizeNeeded;
  150.     
  151.     return myPtr;
  152. }
  153.  
  154. /******************************************************
  155.  *    maDisposePtr.                                                                            *
  156.  *                                                                                                        *
  157.  *    Disposes of a pointer. Make sure you've used            *
  158.  *    MANewPtr to allocate to keep track of memory            *
  159.  *    correctly. Returns NULL for convenience.                    *
  160.  ******************************************************/
  161.  
  162. Ptr maDisposePtr (Ptr doomedPtr)
  163. {
  164.     long        howBig;
  165.     
  166. #if __YOU_WANT_DEBUGGING
  167.     // Null, odd address or unrecognized by Memory Manager are all
  168.     // problems!
  169.     if ((doomedPtr == NULL) || (doomedPtr & 0x01) || (GetPtrSize(doomedPtr) == 0L))
  170.     {
  171.         // Probably should handle this, buddy-boy.
  172.         return NULL;
  173.     }
  174. #endif
  175.  
  176.     gMemoryAllocated -= (howBig = GetPtrSize(doomedPtr));
  177.     gMemPointersAllocated -= howBig;
  178.     DisposePtr(doomedPtr);
  179.     return NULL;
  180. }